<?php
    namespace Home\Controller;
    use Think\Controller;
    class JixiaokeshibeifenController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $account_date = I('get.account_date');

        $where = "1 = 1 ";

        if(!empty($account_date))
        {
            $account_date = strtotime($account_date);
            $where .= " and account_date = $account_date";
        }

        $data = M("comm.sys_dept_info")->where($where)->limit(($page-1)*$limit,$limit)->select();

        $sys_dept_attr_dict_list = M("comm.sys_dept_attr_dict")->select();
        
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];

            for ($j=0; $j < count($sys_dept_attr_dict_list); $j++) 
            { 

                if($info['dept_type'] == $sys_dept_attr_dict_list[$j]['id'])
                { 
                    $data[$i]['dept_type'] = $sys_dept_attr_dict_list[$j]['dept_type'];
                }
            }
        }

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("comm.sys_dept_info")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("comm.sys_dept_info")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("comm.sys_dept_info")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("comm.sys_dept_info")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function update()
    {
        
    }

    public function bak()
    {
        $sys_dept_dict_list = M("comm.sys_dept_dict")->select();
        $all_count = count($sys_dept_dict_list);
        $success_count = 0;
        $account_date = I('post.account_date');
        $account_date = strtotime($account_date);

        $flag = false;

        $info_list =  M('comm.sys_dept_info')->where("account_date = $account_date")->select();
        if(!empty($info_list))
        {
            $flag = true;
            M('comm.sys_dept_info')->where("account_date = $account_date")->delete();
        }

        for ($i=0; $i < count($sys_dept_dict_list); $i++) 
        { 
            $info = $sys_dept_dict_list[$i];

            $info['account_date'] = $account_date;
            
            unset($info['show_flag']);
            unset($info['numrow']);
            
            $result = M('comm.sys_dept_info')->add($info);

            if($result !== false)
            {
                $success_count++;
            }
        }

        $json['msg'] = "共".$all_count."条数据,成功备份".$success_count."条";
        $this->ajaxReturn($json,"json");
    }

   }